package com.gexin.model.agent;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.agent.base.BaseAgentAccountLog;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

/**
 * 业务名称:  t_agent_account_log(金额操作明细) 
 */
@SuppressWarnings("serial")
public class AgentAccountLog extends BaseAgentAccountLog<AgentAccountLog> {
	public static final AgentAccountLog dao = new AgentAccountLog();
	/**状态 1 首次审核*/
	public static final Integer TYPE_AUDIT = 1;
	/**状态 2 用户充值*/
	public static final Integer TYPE_RECHARGE = 2;
	/**状态 3 用户提现*/
	public static final Integer TYPE_WITHDRAWAL = 3;
	/**状态 4 订单收入*/
	public static final Integer TYPE_ORDERIN = 4;

	/**操作账户类型   1  操作货款余额*/
	public static final Integer ACCOUNTTYPEHK = 1;
	/**操作账户类型   2  操作可用余额*/
	public static final Integer ACCOUNTTYPEYE = 2;
	/**操作账户类型   3  操作押金*/
	public static final Integer ACCOUNTTYPEYJ = 3;
	/**操作账户类型   4  操作押货金额*/
	public static final Integer ACCOUNTTYPEYHJE = 4;




	/**状态 1 添加*/
	public static final Integer STATEIN = 1;
	/**状态 0 减少*/
	public static final Integer STATEOUT = 0;

	/**状态 1成功*/
	public static final Integer STATUS_SUCCESS = 1;
	/**状态 2处理中*/
	public static final Integer STATUS_INTO = 2;
	/**状态 3失败*/
	public static final Integer STATUS_FAIL = 3;

	/**是否特约代理商订单 是*/
	public static final Integer TEYUE_TRUE=1;
	/**是否特约代理商订单 不是*/
	public static final Integer TEYUE_FALSE=0;

	/**
	 * 获取流水信息
	 * @param page
	 * @param size
	 * @param agentId
	 * @param state
	 */
	public Page<AgentAccountLog> findAccountLogList(int page , int size , String agentId , Integer state,Integer status) {
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_agent_account_log where agent_id=? ");
		if(state!=null){
			sql.whereEquals("state",state);
		}
		if(status!=null){
			sql.whereEquals("status",status);
		}
		sql.append(" order by create_time desc ");
		return paginate(page,size,"select * ",sql.toString(),agentId);
	}

	/**
	 * 获取所有的提现信息
	 */
	public Page<AgentAccountLog> findWithdrawal(int page, int size) {
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_agent_account_log log ");
		sql.append(" LEFT JOIN t_agent agent ON log.agent_id=agent.id ");
		sql.append(" where log.account_type=2 and log.type=3 and log.`status`=2 ");

		return paginate(page,size,"select log.*,agent.agent_name as agentName ",sql.toString());
	}

	@Override
	public boolean update(){
		this.setEndTime(new Date());
		return super.update();
	}

	@Override
	public boolean save(){
		this.setEndTime(new Date());
		return super.save();
	}

	public Page<AgentAccountLog> findAccountLogErJiList(int page , int size , String agentId , Integer state , Integer status) {
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_agent_account_log  ");

		sql.append(" where agent_id=? OR agent_id in (select id from t_agent where parent_id=(select id from t_agent where id=?)) ");

		if(state!=null){
			sql.whereEquals("state",state);
		}
		if(status!=null){
			sql.whereEquals("status",status);
		}
		sql.append(" order by create_time desc ");
		return paginate(page,size,"select * ",sql.toString(),agentId,agentId);

	}
}
